﻿/*  ToolsMK - Switch
 *  
 *  Copyright (C) 2009  Maik Kellerhals
 *  
 * This file is part of ToolsMK.
 *  
 *  ToolsMK is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package mk.objectsMK{
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.Sprite;
	import flash.display.Shape;
	import mk.mathMK.*;
	
	public class Switch extends Sprite {
		
		var status:Boolean;
		var description:String;
		
		var descriptionTxt:QuickText;
		
		var border:Sprite;
		
		public function Switch(txt:String="", stat:Boolean=true) {
			status = stat;
			description = txt;
			
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOut);
			
			descriptionTxt = new QuickText(0xFFFFFF, 13, "Arial");
			descriptionTxt.x = 25;
			descriptionTxt.y = 0;
			descriptionTxt.selectable = false;
			descriptionTxt.text = description;
			descriptionTxt.width = descriptionTxt.textWidth + 5;
			descriptionTxt.height = 20;
			
			this.addChild(descriptionTxt);
			
			border = new Sprite();
			
			border.graphics.lineStyle(2,0xFFFFFF, 1);
			border.graphics.beginFill(0xFFFFFF, 0);
			border.graphics.drawRoundRect(0,0,20,20,8);
			
			border.alpha = .5;
			
			this.addChild(border);
			
			draw();
			
			
			
		}
		
		private function reDraw():void {
			this.graphics.clear();
			draw();
		}
		
		private function draw():void {
			if (status == true) {
				this.graphics.lineStyle(4,0x00FF00,1);
				this.graphics.moveTo(5,10);
				this.graphics.lineTo(10,15);
				this.graphics.lineTo(15,5);
			} else {
				this.graphics.lineStyle(4,0xFF0000,1);
				this.graphics.moveTo(5,5);
				this.graphics.lineTo(15,15);
				this.graphics.moveTo(5,15);
				this.graphics.lineTo(15,5);
			}
			
			
		}
		
		private function onClick(event:MouseEvent):void {
			if (status == true) {
				dispatchEvent(new Event("off"));
				trace("off dispatched");
				status = false;
			} else {
				dispatchEvent(new Event("on"));
				trace("on dispatched");
				status = true;
			}
			reDraw();
		}
		
		private function onOver(event:MouseEvent):void {
			border.alpha = 1;
		}
		private function onOut(event:MouseEvent):void {
			border.alpha = .5;
		}
	}
}